package edu.sandau.b230111.b23011125.Controller;

import edu.sandau.b230111.b23011125.Global.GlobalEnum;
import edu.sandau.b230111.b23011125.Model.UserInfo;
import edu.sandau.b230111.b23011125.Model.UserLogin;
import edu.sandau.b230111.b23011125.Service.UserService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String Index(){
        return "redirect:/login";
    }

    @RequestMapping("/registeruser")
    public String registerUser(){
        return "registeruser";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null!=loggedUserid){ //已经登录
            String isAdmin = (String) request.getSession().getAttribute("isAdmin");
            if(null!=isAdmin){ //管理员
                return "redirect:/adminhomepage";
            }
            return "redirect:/userhomepage";
        }

        return "login";
    }

    @RequestMapping("/userhomepage")
    public String userHomepage(ModelMap map,HttpServletRequest request,HttpServletResponse response){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        map.put("userid",loggedUserid);
        map.put("name",userService.queryUserInfoByUserId(loggedUserid).getName());
        map.put("school",userService.queryUserInfoByUserId(loggedUserid).getSchool());
        return "userhomepage";
    }

    @RequestMapping("/adminhomepage")
    public String adminHomepage(ModelMap map,HttpServletRequest request,HttpServletResponse response){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        String isadmin = (String)request.getSession().getAttribute("isAdmin");
        if(null==isadmin){//非管理员
            request.getSession().removeAttribute("userid");
            request.getSession().removeAttribute("isAdmin");
            return "redirect:login";
        }
        map.put("userid",loggedUserid);
        map.put("name",userService.queryUserInfoByUserId(loggedUserid).getName());
        map.put("school",userService.queryUserInfoByUserId(loggedUserid).getSchool());
        return "adminhomepage";
    }

    @RequestMapping("/modifyuserlogin")
    public String modifyUserLogin(HttpServletRequest request, ModelMap map){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        UserLogin userlogin = userService.queryUserLoginByUserId(loggedUserid);
        map.put("account",userlogin.getAccount());
        map.put("password",userlogin.getPassword());
        return "modifyuserlogin";
    }

    @RequestMapping("/modifyuserinfo")
    public String modifyUserInfo(HttpServletRequest request, ModelMap map){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        UserInfo userInfo = userService.queryUserInfoByUserId(loggedUserid);
        map.put("name",userInfo.getName());
        map.put("school",userInfo.getSchool());
        return "modifyuserinfo";
    }

    @RequestMapping("/api/adduser")
    public String addUser(HttpServletResponse response, @RequestParam(value="account")String account, @RequestParam(value ="password") String password) throws Exception {
        UserLogin newUserLogin = new UserLogin();
        newUserLogin.setAccount(account);
        newUserLogin.setPassword(password);
        newUserLogin.setIsadmin(0);
        GlobalEnum.OperateResult result =  userService.addUser(newUserLogin);

        if(result== GlobalEnum.OperateResult.UNSUCC){
            System.out.println("UNSUCC");
            response.setContentType("text/html;charset=gb2312");
            PrintWriter out = response.getWriter();
            out.print("<script language=\"javascript\">alert('创建失败！');window.location.href='/registeruser'</script>");
            return "waitpage";
        }

        return "redirect:/login";
    }

    @RequestMapping("/api/logincheck")
    public String loginCheck(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="account")String account, @RequestParam(value ="password") String password) throws Exception {
        UserLogin userLogin = userService.queryUserLoginByAccount(account);
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        if(null==userLogin||!password.equals(userLogin.getPassword())){  //账号密码错误
            out.print("<script language=\"javascript\">alert('账号密码错误！');window.location.href='/login'</script>");
            return "waitpage";
        }
        request.getSession().setAttribute("userid",userLogin.getUserid());

        if(userLogin.getIsadmin()==1){
            request.getSession().setAttribute("isAdmin","YES");
            return "redirect:/adminhomepage";
        }
        return "redirect:/userhomepage";
    }

    @RequestMapping("/api/modifyuserlogin")
    public String modifyUserLogin(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="account")String account, @RequestParam(value ="password") String password) throws IOException {
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        String isadmin = (String)request.getSession().getAttribute("isAdmin");

        UserLogin newUserLogin = new UserLogin();
        newUserLogin.setUserid(loggedUserid);
        newUserLogin.setAccount(account);
        newUserLogin.setPassword(password);
        if(null!=isadmin){
            newUserLogin.setIsadmin(1);
        }
        else{
            newUserLogin.setIsadmin(0);
        }
        GlobalEnum.OperateResult ans =  userService.modifyUserLogin(newUserLogin);
        if(ans== GlobalEnum.OperateResult.UNSUCC){   //修改错误
            response.setContentType("text/html;charset=gb2312");
            PrintWriter out = response.getWriter();
            out.print("<script language=\"javascript\">alert('修改失败！');window.location.href='/userhomepage'</script>");
            return "waitpage";
        }
        request.getSession().removeAttribute("userid");
        request.getSession().removeAttribute("isAdmin");
        return "redirect:/login";
    }

    @RequestMapping("/api/modifyuserinfo")
    public String modifyUserInfo(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="name")String name, @RequestParam(value ="school") String school) throws IOException {
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        UserInfo newUserInfo = userService.queryUserInfoByUserId(loggedUserid);
        if(null==newUserInfo){ //修改失败
            response.setContentType("text/html;charset=gb2312");
            PrintWriter out = response.getWriter();
            out.print("<script language=\"javascript\">alert('修改失败！');window.location.href='/userhomepage'</script>");
            return "waitpage";
        }
        newUserInfo.setSchool(school);
        newUserInfo.setName(name);
        GlobalEnum.OperateResult ans =  userService.modifyUserInfo(newUserInfo);
        if(ans== GlobalEnum.OperateResult.UNSUCC){   //修改错误
            response.setContentType("text/html;charset=gb2312");
            PrintWriter out = response.getWriter();
            out.print("<script language=\"javascript\">alert('修改失败！');window.location.href='/userhomepage'</script>");
            return "waitpage";
        }

        if(newUserInfo.getIsAdmin()==0){
            return "redirect:/userhomepage";
        }
        return "redirect:/adminhomepage";
    }

    @RequestMapping("/api/logout")
    public String logout(HttpServletRequest request){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        request.getSession().removeAttribute("userid");
        request.getSession().removeAttribute("isAdmin");
        return "redirect:/login";
    }

    @RequestMapping("/api/deleteuserlogin")
    public String deleteUserLogin(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        GlobalEnum.OperateResult result = userService.deleteUser(loggedUserid);
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        if(result==GlobalEnum.OperateResult.UNSUCC){ //删除失败

            String isadmin = (String)request.getSession().getAttribute("isAdmin");
            if(null==isadmin){//非管理员
                out.print("<script language=\"javascript\">alert('删除失败！');window.location.href='/userhomepage'</script>");
                return "waitpage";
            }
            out.print("<script language=\"javascript\">alert('删除失败！');window.location.href='/adminhomepage'</script>");
            return "waitpage";
        }

        request.getSession().removeAttribute("userid");
        request.getSession().removeAttribute("isAdmin");
        out.print("<script language=\"javascript\">alert('已删除！');window.location.href='/login'</script>");
        return "waitpage";
    }
}
